package org.example.base.common.controller;

import org.example.base.common.model.TRoleModel;
import org.example.base.common.model.TSystemDictItemModel;
import org.example.base.common.model.TsSystemDictModel;
import org.example.base.common.service.TsSystemDictService;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.exception.ErrorException;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Date: 2024/1/14 18:32
 */
@RestController
@RequestMapping("/dict")
public class TSystemDictController extends BaseController<String, TsSystemDictService, TsSystemDictModel> {

    @Autowired
    private TsSystemDictService dictService;

    @GetMapping("/getByCode")
    private ApiResult getByCode(@RequestParam String code) {
        if (!StringUtils.hasText(code)){
            throw new ErrorException("code不能为空！");
        }
        List<TSystemDictItemModel> dictItemModelList = dictService.getByCode(code);
        return ApiResult.success(dictItemModelList);
    }

    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) Map<String, Object> params) {
        SearchPageResult<TsSystemDictModel> result = new SearchPageResult<>();
        Object name = params.get("name");
        Where where  = Where.build().field("del_flag").equal(0);
        if (!ObjectUtils.isEmpty(name)){
            where.and().field("name").equal(name);
        }
        int totalCount = dictService.count(where);
        List<TsSystemDictModel> resultList = dictService.search(where.limit(page, limit));
        result.setPage(page);
        result.setLimit(limit);
        result.setTotalCount(totalCount);
        result.setList(resultList);
        return ApiResult.success(result);
    }
}
